/*
 * Copyright (c) 2010-2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/opengl/GLTextureUnit.h"

namespace alkes {

const static GLint s_filters[] = {
    GL_NEAREST, // AL_TEXFILTER_VALUE_POINT
    GL_LINEAR,  // AL_TEXFILTER_VALUE_BILINEAR
};

const static GLint s_wraps[] = {
    GL_REPEAT,  // AL_TEXADDRESS_MODE_DISABLE, dummy
    GL_REPEAT,  // AL_TEXADDRESS_MODE_WRAP
    GL_CLAMP_TO_EDGE,   // AL_TEXADDRESS_MODE_CLAMP
};

#define UNIT_COUNT  (sizeof(units_)/sizeof(units_))

GLTextureUnit::GLTextureUnit()
{
    int i;
    for (i = 0; i < UNIT_COUNT; ++i)
    {
        units_[i].min_filter_ = AL_TEXFILTER_VALUE_POINT;
        units_[i].mag_filter_ = AL_TEXFILTER_VALUE_POINT;
        units_[i].u_mode_ = AL_TEXADDRESS_MODE_DISABLE;
        units_[i].v_mode_ = AL_TEXADDRESS_MODE_DISABLE;
    }
}

GLTextureUnit::~GLTextureUnit()
{
}

void GLTextureUnit::setTextureFilter(int stage, TexFilterValue min_filter, TexFilterValue mag_filter)
{
    AL_PRE_COND(stage < UNIT_COUNT, );

    units_[stage].min_filter_ = min_filter;
    units_[stage].mag_filter_ = mag_filter;
}

void GLTextureUnit::getTextureFilter(int stage, TexFilterValue* min_filter, TexFilterValue* mag_filter)
{
    AL_PRE_COND(stage < UNIT_COUNT, );

    if (min_filter)
        *min_filter = units_[stage].min_filter_;

    if (mag_filter)
        *mag_filter = units_[stage].mag_filter_;
}

void GLTextureUnit::setTextureAddressMode(int stage, TexAddressMode u_mode, TexAddressMode v_mode)
{
    AL_PRE_COND(stage < UNIT_COUNT, );

    units_[stage].u_mode_ = u_mode;
    units_[stage].v_mode_ = v_mode;
}

void GLTextureUnit::getTextureAddressMode(int stage, TexAddressMode* u_mode, TexAddressMode* v_mode)
{
    AL_PRE_COND(stage < UNIT_COUNT, );

    if (u_mode)
        *u_mode = units_[stage].u_mode_;

    if (v_mode)
        *v_mode = units_[stage].v_mode_;
}

void GLTextureUnit::activate(int stage)
{
    AL_PRE_COND(stage < UNIT_COUNT, );

    const Unit& unit = units_[stage];

    AL_OGL_INVOKE(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, s_filters[unit.mag_filter_]));
    AL_OGL_INVOKE(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, s_filters[unit.min_filter_]));

    if (unit.u_mode_ != AL_TEXADDRESS_MODE_DISABLE)
    {
        AL_OGL_INVOKE(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, s_wraps[unit.u_mode_]));
    }

    if (unit.v_mode_ != AL_TEXADDRESS_MODE_DISABLE)
    {
        AL_OGL_INVOKE(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, s_wraps[unit.v_mode_]));
    }
}

}
